/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted


/// Generating total Nonwhite respondants by year

by year: egen nonwhiteresp = count(race) if race==4 | race== 5 | race==3

/// Generating total number of Nonwhite voters by year


by year: egen Nonwhitevoted = count(voted) if race==1 & voted==3 | race==2 & voted==3 | race==5 & voted==3

/// Generating total number of Latnio non-voters by year

by year: egen NonwhiteNvoted = count(voted) if voted==0 & race==3 | voted==1 & race==3 | voted==2 & race==3 | voted==0 & race==4 | voted==1 & race==4 | voted==2 & race==4 | voted==0 & race==5 | voted==1 & race==5 | voted==2 & race==5 


/// Generating total number of non-Nonwhite voters by year

by year: egen NNonwhitevoted = count(voted) if race==1 & voted==3 | race==2 & voted==3

/// Generating total number of non-Nonwhite non-voters by year

by year: egen NNonwhiteNvoted = count(voted) if race==1 & voted==0 | race==1 & voted==1 | race==1 & voted==2 | race==2 & voted==0 | race==2 & voted==1 | race==2 & voted==2

/// filling in the blanks

by year: egen nonwhitevoted = max(Nonwhitevoted)
by year: egen nonwhiteNvoted = max(NonwhiteNvoted)
by year: egen Nnonwhitevoted = max(NNonwhitevoted)
by year: egen NnonwhiteNvoted = max(NNonwhiteNvoted)




/// Generating % Nonwhite TO with Mosteller Correction

gen N = 1
gen a = [nonwhitevoted * NnonwhiteNvoted] / [nonwhiteNvoted * Nnonwhitevoted]


///Generating filler vars

gen aminone =(a-1)
gen ANonWhiteplusTO = ((1-AWhite-ABlack)+aTO)
gen aNonwhiteTO=(a*(1-AWhite-ABlack)*aTO)

gen CNonWhiteTO = 1/ (2*(aminone)) * (N+(aminone*ANonWhiteplusTO) - sqrt((N+aminone*ANonWhiteplusTO)*(N+aminone*ANonWhiteplusTO) - (4*(aminone*aNonwhiteTO))))

///Adjusting TO for the formula

replace CNonWhiteTO = CNonWhiteTO/(1-AWhite-ABlack)



/// Moving to Partisanship - Generating Nonwhite Democratic Vote total by year

by year: egen Nonwhitedem = count(race) if presvote==1 & race==3 | presvote==1 & race==4 | presvote==1 & race==5

/// Generating Nonwhite Non-Democratic Vote by Year

by year: egen NonwhiteNdem = count(race) if presvote==2 & race==3 | presvote==2 & race==4 | presvote==2 & race==5

// Generating non-Nonwhite Democratic Vote total by year

by year: egen NNonwhitedem = count(race) if presvote==1 & race==1 | presvote==1 & race==2

/// Generating non-Nonwhite non-Democratic vote total by year

by year: egen NNonwhiteNdem = count(race) if presvote==2 & race==1 | presvote==2 & race==2
 
/// filling in the blanks

by year: egen nonwhiteDem = max(Nonwhitedem)
by year: egen nonwhiteNDem = max(NonwhiteNdem)
by year: egen NnonwhiteDem = max(NNonwhitedem)
by year: egen NnonwhiteNDem = max(NNonwhiteNdem)

/// Generation % Nonwhite Democrat with Mosteller Correction

gen A = (nonwhiteDem*NnonwhiteNDem)/(nonwhiteNDem*NnonwhiteDem)
gen Aminone =(A-1)
gen ANonWhiteplusDem = ((1-AWhite-ABlack)+ADem)
gen ANonWhiteDem=(A*(1-AWhite-ABlack)*ADem)
gen CNonWhiteDem = 1/(2*(Aminone)) * (N+(Aminone*ANonWhiteplusDem) - sqrt((N+Aminone*ANonWhiteplusDem)*(N+Aminone*ANonWhiteplusDem) - (4*(Aminone*ANonWhiteDem))))

replace CNonWhiteDem = CNonWhiteDem/(1-AWhite-ABlack)


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen nonwhitecoalcontrib = (CNonWhiteTO* CNonWhiteDem *(1-AWhite-ABlack))/(aTO*ADem)



